package com.moyq5.permit.center.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.embed.ShopAdminId;
import com.moyq5.permit.center.bean.entity.Shop;
import com.moyq5.permit.center.bean.entity.ShopAdmin;
import com.moyq5.permit.center.bean.search.ShopAdmin4Search;
import com.moyq5.permit.center.feign.client.ShopAdminFeign;
import com.moyq5.permit.center.web.jackson.filter.ShopAdminFilter;

/**
 * 门店管理员
 * @author Moyq5
 * @date 2019年7月25日
 */
@RestController
@RequestMapping("/shopAdmin")
public class ShopAdminController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(ShopAdminController.class);
	
	@Autowired
	private ShopAdminFeign shopAdminFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年7月30日
	 * @param shopAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String page(ShopAdmin4Search shopAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询代理商管理员信息>>>page(MerchAdmin4Search, PageData)");
		if (null != getLoginShopId()) {
			if (null == shopAdmin4s.getShop()) {
				shopAdmin4s.setShop(new Shop());
			}
			shopAdmin4s.getShop().setShopId(getLoginShopId());;
		}
		return toJson(shopAdminFeign.page(new Body<ShopAdmin4Search, PageData>(shopAdmin4s, pd)), ShopAdminFilter.get());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/admin", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithAdmin(ShopAdmin4Search shopAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询门店管理员信息>>>pageWithAdmin(ShopAdmin4Search, PageData)");
		if (null != getLoginShopId()) {
			if (null == shopAdmin4s.getShop()) {
				shopAdmin4s.setShop(new Shop());
			}
			shopAdmin4s.getShop().setShopId(getLoginShopId());;
		}
		return toJson(shopAdminFeign.pageWithAdmin(new Body<ShopAdmin4Search, PageData>(shopAdmin4s, pd)), ShopAdminFilter.getWithAdmin());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/shopAndAdmin", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithShopAndAdmin(ShopAdmin4Search shopAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询门店管理员信息>>>pageWithShopAndAdmin(ShopAdmin4Search, PageData)");
		if (null != getLoginShopId()) {
			if (null == shopAdmin4s.getShop()) {
				shopAdmin4s.setShop(new Shop());
			}
			shopAdmin4s.getShop().setShopId(getLoginShopId());;
		}
		return toJson(shopAdminFeign.pageWithShopAndAdmin(new Body<ShopAdmin4Search, PageData>(shopAdmin4s, pd)), ShopAdminFilter.getWithShopAndAdmin());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopAdmin
	 * @return
	 */
	@PutMapping("/add")
	@ResponseBody
	public ObjectResult<ShopAdminId> add(@RequestBody ShopAdmin shopAdmin) {
		log.debug("添加门店管理员信息>>>add(ShopAdmin)");
		if (null != getLoginShopId()) {
			shopAdmin.getId().setShopId(getLoginShopId());
		}
		ObjectResult<ShopAdmin> objRst = shopAdminFeign.one(shopAdmin.getId());
		if (objRst.getStatus() == Status.OK && null != objRst.getData()) {
			ObjectResult<ShopAdminId> idRst = new ObjectResult<ShopAdminId>();
			idRst.setOk();
			idRst.setData(shopAdmin.getId());
			return idRst;
		}
		return shopAdminFeign.create(shopAdmin);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopAdmin
	 * @return
	 */
	@PatchMapping("/edit")
	@ResponseBody
	public StatusResult edit(@RequestBody @Validated ShopAdmin shopAdmin) {
		log.debug("更新门店管理员信息>>>edit(ShopAdmin)");
		if (null != getLoginShopId()) {
			shopAdmin.getId().setShopId(getLoginShopId());
		}
		return shopAdminFeign.update(shopAdmin);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param shopAdminId
	 * @return
	 */
	@DeleteMapping("/del")
	@ResponseBody
	public StatusResult del(@RequestBody ShopAdminId shopAdminId) {
		log.debug("删除门店管理员信息>>>del(ShopAdminId)");
		if (null != getLoginShopId()) {
			shopAdminId.setShopId(getLoginShopId());
		}
		return shopAdminFeign.remove(shopAdminId);
	}
	
}
